<?php
/**
 * 
 *海报管理报表
 * 
 */
 
use Workerman\Worker;
use Workerman\Mysql;

 class mod_poster_information_management{
    
    public function entry($param){
        
        return $this->do_mod_poster_information_management($param);
    }

    
    /** 
    * do_mod_poster_information_management  
    * 
    * 
    * @access private 
    * @param $param batch
    * @since 1.0 
    * @return array(errcode,errmsg,rtv) 
    */
    private function do_mod_poster_information_management($param){
        $action = $_POST["action"];
    
        if(empty($action) || $action==""){
            
            $tm_from = @$_POST["tm_from"];
            $tm_to = @$_POST["tm_to"];
            $order_status = @$_POST["order_status"];
            
            $query_criteria = " ";
            // 如果是按日期查询订单信息，则加上where条件
            if($tm_from != NULL && $tm_to != NULL){
                $tm_from .=" 00:00:00";
                $tm_to .=" 23:59:59";
                if($order_status == ""){
                    $query_criteria .= "WHERE a.order_time>='$tm_from' AND a.order_time<='$tm_to' GROUP BY a.id";
                }else{
                    $query_criteria .= "WHERE a.order_time>='$tm_from' AND a.order_time<='$tm_to' AND a.order_status='$order_status' GROUP BY a.id";
                }
            }
            
            $sql = "select a.id,a.name,a.status,if(count(b.number) =0,1,count(b.number)) as number,a.start_time,a.end_time FROM hf_mes_poster_management a 
LEFT JOIN hf_mes_poster_contents b ON a.id=b.poster_id 

GROUP BY a.id
                $query_criteria ORDER BY id DESC;";

            $dp=sql_query($sql);

            foreach($dp as $key=> $values){
                $poster_contents_id = $values["id"];
                $sql = "SELECT finish,`lock` FROM hf_mes_poster_contents WHERE poster_id='$poster_contents_id' ORDER BY id DESC  LIMIT 1 ";
                $ret =  sql_query($sql);
                if(count($ret) <= 0){
                    $dp[$key]["finish"] = 0;
                    $dp[$key]["lock"] = 0;
                }else{
                    $finish = $ret[0]["finish"] == 1 ? $ret[0]["finish"]:0;
                    $lock = $ret[0]["lock"] == 1 ? $ret[0]["lock"]:0;

                    // 如果上一次稿件已校稿完成，则可解锁上传海报和绑定部门
                    if($finish == 1 && $lock == 1 && $values["number"] <1){
                        $dp[$key]["finish"] = $finish;
                        $dp[$key]["lock"] = 0;
                    }else{
                        $dp[$key]["finish"] = $finish;
                        $dp[$key]["lock"] = $lock;
                    }
                }
            }

            $obj = new stdClass();
            $obj-> data = $dp;
        }


        if($action == "add"){
            $name = $_POST["name"];
            $end_time = $_POST["end_time"];
            
            $poster_id = sql_insert("hf_mes_poster_management",array('name'=>$name,'status'=>'proofreading','start_time'=>date("Y-m-d H:i:s"),'end_time'=>$end_time));
            $obj = "done";
            
        }

        if($action == "edit"){
            $id = $_POST["id"];
            $name = $_POST["name"];
            $end_time = $_POST["end_time"];
            
            sql_update("hf_mes_poster_management",array('name'=>$name,'end_time'=>$end_time), array('id'=>$id));
            $obj = "done";
        }

        if($action == "delete"){
            $id = $_POST["id"];
            
            sql_delete("hf_mes_poster_management", array('id'=>$id));
            $obj = "done";
        }

        /*获取部门信息*/
        if($action == "get_department_info"){
            $sql = "SELECT id, name FROM hf_mes_department;";
            $dp=sql_query($sql);
            //print_r($dp);
            //$obj = "{\"data\": " . json_encode($dp) . "}";
            $obj = new stdClass();
            $obj-> data = $dp;
        }
        
        // 上传海报图片
        if($action == "upload_poster_image"){
            
            try{
                sql_beginTrans();
                $poster_id = $_POST["poster_id"];
                $page = $_POST["page"];
                if($_POST["page"] == 'null'){
                    $page = 1;
                }

                if($page < 1){
                    $obj['error'] = "页码错误";
                    throw new Exception(json_encode($obj));
                }

                // 如果上传文件文件不存在，则抛出异常
                if(!isset($_FILES[0])){
                    $obj = [];
                    $obj['error'] = "No file found";
                    throw new Exception(json_encode($obj));
                }

                $number = 1;
                $sql = "select id,number,finish,`lock` from hf_mes_poster_contents where poster_id='$poster_id' order by id desc limit 1";
                $dp=sql_query($sql);
                
                if(count($dp) > 0){
                    $obj = [];
                    if($dp[0]["finish"] == "1" && $dp[0]["number"] >= 1){
                        $obj['error'] = '该海报已校稿1次，不允许上传海报图片';
                        throw new Exception(json_encode($obj));

                    }elseif($dp[0]["lock"] == "1" && $dp[0]["finish"] == "0"){
                        $obj['error'] = '当次校稿已锁定，不允许上传海报图片';
                        throw new Exception(json_encode($obj));

                    }elseif($dp[0]["lock"] == "1" && $dp[0]["finish"] == "1"){
                        // 上次校稿已完成,需要新建当次校稿
                        $number = $dp[0]["number"] + 1;
                        $poster_contents_id = sql_insert("hf_mes_poster_contents",array('poster_id'=>$poster_id,'number'=>$number));

                    }elseif($dp[0]["lock"] == "0" && $dp[0]["finish"] == "0"){
                        // 当次校稿未锁定，可以继续上传海报图片
                        $poster_contents_id = $dp[0]["id"];
                    }

                }else{
                    $poster_contents_id = sql_insert("hf_mes_poster_contents",array('poster_id'=>$poster_id,'number'=>$number));
                }
                //var_dump($poster_contents_id);


                // 判断当次校稿是否已存在，页、区域海报图片
/*                $sql="SELECT id FROM hf_mes_poster_picture WHERE poster_contents_id='$poster_contents_id' AND page='$page' AND area='$area' LIMIT 1"; */
                $sql="SELECT id FROM hf_mes_poster_picture WHERE poster_contents_id='$poster_contents_id' AND page='$page' LIMIT 1";
                $dp=sql_query($sql);
                if(count($dp)>0){
                    $obj = [];
                    $obj['error'] = "该海报页数和版本已存在海报图片，如需替换海报图片，请先删除";
                    throw new Exception(json_encode($obj));
                }

                $targetDir = G_POSTERIMAGES_DIR .date("Y").'/'.date("m").'/'.date("d");
				//var_dump($targetDir);
                // 查看是否存在目录，不存在则创建,允许递归创建
                if (!file_exists($targetDir)) {
                    $t = @mkdir($targetDir,0777,true);
                }

                $sql_str = "";
                foreach($_FILES as $values){
                    $old_name = $values['file_name'];
                    // 文件名格式为【名称_时间戳.扩展名】
                    //$fileName = substr($old_name,0,strrpos($old_name,".")) ."_".time().".". substr($old_name,strripos($old_name,".")+1);
                    $fileName = guid().".". substr($old_name,strripos($old_name,".")+1);

                    $file_data = $values['file_data'];
                    $file_input_name = $values['name'];
                    
                    $targetFile = $targetDir.'/'.$fileName;
                    if(file_put_contents($targetFile, $file_data) === FALSE){
                        //return array('error' => 'Error uploading chunk ' . fileName); 
                        $obj = [];
                        $obj['error'] = 'Error uploading chunk ' . $fileName;
                        throw new Exception(json_encode($obj));
                    }
                    $picture_path = '/'.date("Y").'/'.date("m").'/'.date("d").'/'.$fileName;
                   //$sql_str .= "($poster_contents_id,'$picture_path','$page','$area'),";
                    $sql_str .= "($poster_contents_id,'$picture_path','$page'),";
                }
                $sql_str = rtrim($sql_str,",");
/*                $sql = "insert into hf_mes_poster_picture (poster_contents_id,picture_path,page,area) values $sql_str"; */
                $sql = "insert into hf_mes_poster_picture (poster_contents_id,picture_path,page) values $sql_str";
                sql_query($sql);
                //return array('chunkIndex' => "success");
                sql_commitTrans();
                $obj = [];
                $obj['chunkIndex'] = "success";
                return array("errcode"=> 0,"errmsg"=>"","rtval"=>$obj);
                
            }catch(Exception $e){
                sql_rollBackTrans();
                return array("errcode"=> 0,"errmsg"=>"","rtval"=>json_decode($e->getMessage()));
            }
            
            
            
        }

        // 上传csv
        if($action == "upload_poster_csv") {
            //var_dump(1111111);die;
            try {
                sql_beginTrans();
                $poster_management_id = $_POST["poster_management_id"];
                $page = $_POST["page"];

                //查询原来的按钮
                $button_sql = "SELECT id FROM hf_mes_poster_button_import
                    WHERE poster_management_id='$poster_management_id' 
                    AND page='$page';
                    ";
                $button_dp=sql_query($button_sql);
                if(count($button_dp) > 0){
                    $delete_button_sql = "DELETE FROM hf_mes_poster_button_import
                    WHERE poster_management_id='$poster_management_id' 
                    AND page='$page'";
                    sql_query($delete_button_sql);
                }

                $sql = "SELECT a.id FROM hf_mes_poster_contents a
                    LEFT JOIN hf_mes_poster_picture d ON d.poster_contents_id=a.id
                    LEFT JOIN hf_mes_poster_picture_department c ON c.picture_id=d.id
                    WHERE a.poster_id='$poster_management_id'
                    AND d.page='$page'
                    ";

                $dp=sql_query($sql);
                //var_dump($dp);die;
                if(count($dp) <=0){
                    return array("errcode"=> 0,"errmsg"=>"","rtval"=>"找不到綁定部門信息");
                }

                $targetDir = G_CSV_DIR .date("Y").'/'.date("m").'/'.date("d");

                if (!file_exists($targetDir)) {
                    $t = @mkdir($targetDir,0777,true);
                }

                foreach($_FILES as $values){
                    $old_name = $values['file_name'];
                    // 文件名格式为【名称_时间戳.扩展名】

                    $fileName = guid().".". substr($old_name,strripos($old_name,".")+1);

                    $file_data = $values['file_data'];

                    $targetFile = $targetDir.'/'.$fileName;
                    if(file_put_contents($targetFile, $file_data) === FALSE){
                        //return array('error' => 'Error uploading chunk ' . fileName);
                        $obj = [];
                        $obj['error'] = 'Error uploading chunk ' . $fileName;
                        throw new Exception(json_encode($obj));
                    }
                    $picture_path = G_CSV_DIR.date("Y").'/'.date("m").'/'.date("d").'/'.$fileName;
                    break;
                }


// 以只读的方式打开文件
                $handle = fopen($picture_path, 'r');

                if($handle === FALSE) die("打开文件资源失败");

// setlocale() 函数设置地区信息（地域信息）。
                //@setlocale(LC_ALL, 'zh_CN');

// CSV对应的字段名
                $csv_val = array( 'sku', 'unit', 'price', 'name', 'desc', 'page', 'idx', 's_price', 'limit', 'area_time', 'mark', 'origin', 'flag','department_name');
                $csv_arr = array();

                setlocale(LC_ALL, 'zh_CN');
                while (($data = fgetcsv($handle)) !== FALSE) {
                    // TODO=>=>这里需要检查和给的字段数是否一致，如果不一致，则不能写入
                    $tmp_row = array();

                    foreach ($csv_val as $k => $v) {
                        $res = mb_detect_encoding($data[$k],'ASCII,UTF-8,GB2312,EUC-CN,GBK,BIG5');

                        if(!$res){
                            return array("errcode"=> 0,"errmsg"=>"","rtval"=>'不要上传奇怪的编码');
                        }
                        $bm = $res;

                        $tmp_row[$v] = mb_convert_encoding($data[$k], 'utf8',$bm);
                        $aa[$v] =$bm;

                    }

                    $csv_arr[] = $tmp_row;
                }

// 关闭资源
                fclose($handle);

                unset($csv_arr[0]);
                //var_dump(json_decode(json_encode($csv_arr)));die;
                //获取部门ID
                $department= array_column($csv_arr, 'department_name');
                $department = array_unique($department);

                $department_str='';
                foreach ($department as $kk=>$vv){
                    $department_str .= '\''.$vv.'\''.',';
                }
                $department_str = rtrim($department_str,",");

                $sql = "SELECT id,name FROM hf_mes_department WHERE name IN ($department_str) GROUP BY name";

                $departmentDb = sql_query($sql);

                $department_array = array_column($departmentDb,NULL,'name');

// TODO::判断数据是否读取到，然后入库
                if (!empty($csv_arr)) {
                    $sql_str = "";
                    $ids = [];
                    foreach ($csv_arr as $k => $v) {
                        if($ids[$v['idx']]){
                            continue;
                        }
                        //跳出本次循环 重复的只要一个
                        $ids[$v['idx']] = $v['idx'];
                        $button_json = json_encode($v,JSON_UNESCAPED_UNICODE);

                        $button = [
                            "type" => "i-text",
                            "version" => "4.0.0-beta.8",
                            "originX" => "left",
                            "originY" => "top",
                            "left" => 308,
                            "top" => 176,
                            "width" => 166.13,
                            "height" => 72.32,
                            "fill" => "#F00",
                            "stroke" => null,
                            "strokeWidth" => 1,
                            "strokeDashArray" => null,
                            "strokeLineCap" => "butt",
                            "strokeDashOffset" => 0,
                            "strokeLineJoin" => "miter",
                            "strokeMiterLimit" => 4,
                            "scaleX" => 1,
                            "scaleY" => 1,
                            "angle" => 0,
                            "flipX" => false,
                            "flipY" => false,
                            "opacity" => 1,
                            "shadow" => null,
                            "visible" => true,
                            "backgroundColor" => "rgba(255,255,255,0.8)",
                            "fillRule" => "nonzero",
                            "paintFirst" => "fill",
                            "globalCompositeOperation" => "source-over",
                            "skewX" => 0,
                            "skewY" => 0,
                            "text" => " 序号{$v['idx']} ",
                            "fontSize" => 64,
                            "fontWeight" => "bold",
                            "fontFamily" => "Comic Sans",
                            "fontStyle" => "normal",
                            "lineHeight" => 1.16,
                            "underline" => false,
                            "overline" => false,
                            "linethrough" => false,
                            "textAlign" => "left",
                            "textBackgroundColor" => "",
                            "charSpacing" => 0,
                            "styles" => [
                            ],
                            "is_confirm"=>false,
                            "info" => $v,
                        ];

                        $create_date = date("Y-m-d H=>i=>s");

                        $button = json_encode($button,JSON_UNESCAPED_UNICODE);

                        //插入ID

                        if(!empty($department_array[$v['department_name']])){
                            $department_id = $department_array[$v['department_name']]['id'];
                        }else{
                            //找不到部门 不入库
                            continue;
                        }

                        $sql_str .= "($poster_management_id,'$button_json','$button','$page','{$v['department_name']}','$department_id','$create_date'),";
                    }

                    $sql_str = rtrim($sql_str, ",");
                    $sql = "insert into hf_mes_poster_button_import (poster_management_id,import_json,button_json,page,department_name,department_id,create_date) values $sql_str";

                    $res = sql_query($sql);

                    sql_commitTrans();

                    $obj = [];
                    $obj['chunkIndex'] = "success";

                    return array("errcode" => 0, "errmsg" => "", "rtval" => $obj);

                }
            } catch (Exception $e) {
                sql_rollBackTrans();
                return array("errcode" => 0, "errmsg" => "", "rtval" => json_decode($e->getMessage()));
            }

        }
        
        // 获取页数信息(当次校稿未锁定的稿件)
        // {poster_contents_id:"1",page_arr:[1,2,3]}
        if($action == "get_page_info"){
            $poster_id = $_POST["poster_id"];
            $obj = new stdClass();
            $sql = "SELECT id AS poster_contents_id,finish,`lock` FROM hf_mes_poster_contents WHERE poster_id='$poster_id' AND `lock`=0 ORDER BY id DESC LIMIT 1";
            $dp=sql_query($sql);
            if(count($dp)>0){
                $poster_contents_id = $dp[0]["poster_contents_id"];

 /*               $sql = "SELECT id,picture_path,`page`,area FROM hf_mes_poster_picture WHERE poster_contents_id='$poster_contents_id'";  */
                $sql = "SELECT id,picture_path,`page` FROM hf_mes_poster_picture WHERE poster_contents_id='$poster_contents_id'";
                $dp=sql_query($sql);
                if(count($dp)>0){
                    $obj->poster_contents_id = $poster_contents_id;
                    $obj->page_arr = [];
                    foreach($dp as $values){
                        $page = $values["page"];
                       // $area = $values["area"];

                        if(!in_array($page,$obj->page_arr)){
                            array_push($obj->page_arr,$page);
                        }
                    }
                    sort($obj->page_arr);

                }

            }
        }

        // 通过poster_contents_id和page，获取绑定部门和版本图片
        //         {area_arr: {      区
        //             1: {
        //                 picture_id: 3,
        //                 picture_path: "/2020/04/21/dffd.jpg"
        //             },
        //             2: {
        //                 picture_id: 4,
        //                 picture_path: "/2020/04/21/dffd.jpg"
        //             }
        //         },
        //         deparment_arr: [1, 2, 3] 绑定部门
        //         } 
        if($action == "get_department_and_pictures"){
            $poster_contents_id = $_POST["poster_contents_id"];
            $page = $_POST["page"];
            $obj = new stdClass();
            $obj->area_arr = new stdClass();
            $obj->department_arr = [];
            $sql = "SELECT id,picture_path,area FROM hf_mes_poster_picture WHERE poster_contents_id='$poster_contents_id' AND page='$page'";
            $dp=sql_query($sql);
            if(count($dp)>0){
                foreach($dp as $values){
                    $picture_id = $values["id"];
                    $picture_path = $values["picture_path"];
                    $area = $values["area"];
                    $obj_area = new stdClass();
                    $obj_area->picture_id = $picture_id;
                    $obj_area->picture_path = $picture_path;
                    $obj->area_arr->$area = $obj_area;
                    $sql = "SELECT department_id FROM hf_mes_poster_picture_department WHERE picture_id='$picture_id'";
                    $ret=sql_query($sql);
                    foreach($ret as $val){
                        if(!in_array($val["department_id"],$obj->department_arr)){
                            array_push($obj->department_arr,$val["department_id"]);
                        } 
                    }
                    
                }

            }

            // 获取部门信息
            $sql = "SELECT id, name FROM hf_mes_department;";
            $department=sql_query($sql);
            $obj->department_data = $department;

        } 
        
        // 海报页数绑定部门信息
        //1、先判断picture_id对应的hf_mes_poster_contents表中的lock是否锁定？如果锁定，不允许绑定
        //2、如果没锁，则找到poster_contents_id和page一致的所有picture_id
        //3、删除hf_mes_poster_picture_department表所有picture_id的数据
        //4、新增picture_id、department_id
        // {picture_id:2,department_id:[1,2,3]}
        if($action == "set_poster_picture_department"){
            $picture_id = $_POST["picture_id"];
            $department_id = $_POST["department_id"];

            $sql = "SELECT a.poster_contents_id,a.page,b.lock FROM hf_mes_poster_picture a 
                    INNER JOIN hf_mes_poster_contents b ON a.poster_contents_id=b.id 
                    WHERE a.id='$picture_id'";
            $dp=sql_query($sql);
            if(count($dp) <=0){
                return array("errcode"=> 0,"errmsg"=>"","rtval"=>"找不到该页下图片信息");
            }

            if($dp[0]["lock"] == 1){
                return array("errcode"=> 0,"errmsg"=>"","rtval"=>"该期海报已被锁定，不允许更改绑定部门");
            }
            $poster_contents_id = $dp[0]["poster_contents_id"];
            $page = $dp[0]["page"];

            $sql = "SELECT id FROM hf_mes_poster_picture WHERE poster_contents_id='$poster_contents_id' AND page='$page'";
            $dp=sql_query($sql);

            $sql_str = "";
            $picture_department_sql  = "";
            foreach($dp as $val){
                $sql_str .= "".$val["id"].",";
                foreach($department_id as $v){
                    $picture_department_sql .= "('".$val["id"]."',$v,'[]'),";
                }
            }
            $sql_str =  rtrim($sql_str, ",");
            $picture_department_sql =  rtrim($picture_department_sql, ",");

            $sql = "DELETE FROM hf_mes_poster_picture_department WHERE picture_id IN ($sql_str)";
            sql_query($sql);

            $sql = "insert into hf_mes_poster_picture_department (picture_id,department_id,contents_confirm) values $picture_department_sql";
            sql_query($sql);
            $obj = "done";
            
        }

        // 删除海报图片
        // {picture_id:2}
        if($action == "delete_page_area_picture"){
            $picture_id = $_POST["picture_id"];
            $sql = "SELECT a.poster_contents_id,a.page,b.lock FROM hf_mes_poster_picture a 
                    INNER JOIN hf_mes_poster_contents b ON a.poster_contents_id=b.id 
                    WHERE a.id='$picture_id'";
            $dp=sql_query($sql);
            if(count($dp) <=0){
                return array("errcode"=> 0,"errmsg"=>"","rtval"=>"找不到该页下图片信息");
            }

            if($dp[0]["lock"] == 1){
                return array("errcode"=> 0,"errmsg"=>"","rtval"=>"该期海报已被锁定，不允许删除图片");
            }

            $poster_contents_id = $dp[0]["poster_contents_id"];
            $page = $dp[0]["page"];

            $sql = "SELECT id FROM  hf_mes_poster_picture WHERE poster_contents_id = '$poster_contents_id' AND page='$page'";
            $ret = sql_query($sql);

            $sql_str = "";
            foreach($ret as $val){
                $sql_str .= "'".$val["id"]."',";
            }
            $sql_str =  rtrim($sql_str, ",");

            $sql = "DELETE FROM hf_mes_poster_picture_department WHERE picture_id IN ($sql_str)";
            sql_query($sql);

            $sql = "DELETE FROM hf_mes_poster_picture WHERE id IN ($sql_str)";
            sql_query($sql);
            $obj = "done";
            
        }
        
        // 锁定稿件
        // 1、判断存不存未锁定的当次稿件
        // 2、如果存在，判断当次稿件是不是所有页都绑定部门？
        // 3、如果所有页绑定了部门，那就把当次稿件lock锁定
        if($action == "poster_lock"){
            $poster_id = $_POST["poster_id"];
            $sql = "SELECT id AS poster_contents_id FROM hf_mes_poster_contents WHERE poster_id='$poster_id' AND `lock`=0 ORDER BY id DESC LIMIT 1";
            $dp=sql_query($sql);
            if(count($dp) <= 0){
                return array("errcode"=> 0,"errmsg"=>"","rtval"=>'不存在未锁定的当次稿件');
            }

            $poster_contents_id = $dp[0]["poster_contents_id"];
            // 查看当次稿件所有页都是否绑定部门
            $sql = "SELECT id,page FROM hf_mes_poster_picture WHERE poster_contents_id='$poster_contents_id'";
            $ret = sql_query($sql);
            $page_arr = [];
            foreach($ret as $val){
                $picture_id  = $val["id"];
                if(!in_array($val["page"],$page_arr)){
                    $sql = "SELECT id FROM hf_mes_poster_picture_department WHERE picture_id='$picture_id'";
                    $ret = sql_query($sql);
                    if(count($ret)<=0){
                        return array("errcode"=> 0,"errmsg"=>"","rtval"=>'当次稿件第'.$val["page"].'页还未绑定部门，不允许锁定稿件');
                    }
                    array_push($page_arr,$val["page"]);
                }
            }

            $sql = "UPDATE hf_mes_poster_contents SET `lock`=1 WHERE id='$poster_contents_id'";
            sql_query($sql);
            $obj = "done";
        }

        if($action == "send_email"){
            $id = $_POST["id"];
            $sql = "select d.email from hf_mes_poster_contents a 
					LEFT JOIN hf_mes_poster_picture b ON a.id=b.poster_contents_id 
					LEFT JOIN hf_mes_poster_picture_department c ON b.id=c.picture_id 
					INNER JOIN hf_mes_user d ON c.department_id=d.department 
					where a.poster_id='$id'";
            $dp=sql_query($sql);
            if(count($dp) <= 0){
                //return array('rtval'=>'该部门没有绑定员工邮箱信息');
                return array("errcode"=> 0,"errmsg"=>"","rtval"=>'该部门没有绑定员工邮箱信息');
            }
            $email_arr = [];
            foreach($dp as $val){
                array_push($email_arr,$val["email"]);
            }
            $flag = sendMail($email_arr, '有稿件待校', '请用手机登录校稿系统，进行校稿,网址:http://www.sctmes.com:6060/mobile_web/login.php');
            if ($flag) {
                $obj = "done";
            } else {
                $obj = "邮件发送失败！";
            }
        }

        if($action == "get_user_data"){
            $userObj= Token::getToken($_SERVER['HTTP_ACCESS_TOKEN']);
            //var_dump($userObj);die;
            $groups_id = $userObj['data']['user_data'][0]['groups'];
            $sql = "SELECT * FROM hf_mes_groups WHERE id='$groups_id'";
            $obj = sql_query($sql);
            if($obj[0]['class'] == '采购员'){
                $obj[0]['class_level'] = 1;
            }elseif ($obj[0]['class'] == '副总监'){
                $obj[0]['class_level'] = 2;
            }elseif ($obj[0]['class'] == '总监'){
                $obj[0]['class_level'] = 3;
            }elseif ($obj[0]['class'] == '设计师'){
                $obj[0]['class_level'] = 0;
            }

        }

        //获取导入button 的数量
        if($action  == "get_poster_button_number"){
            $poster_management_id = $_POST["poster_management_id"];
            //var_dump($poster_management_id);
            $sql = "SELECT id,page,count(id) as number
                    FROM hf_mes_poster_button_import
                    WHERE poster_management_id='$poster_management_id' GROUP BY page";

            $dp=sql_query($sql);
            $data =[];
            //var_dump($dp);
            if(count($dp) > 0){
                foreach ($dp as $k=>$v){
                    $data[$v['page']] = $v;
                }

            }

            $obj = $data;

        }

        //return array('rtval'=>$obj);
        return array("errcode"=> 0,"errmsg"=>"","rtval"=>$obj);
    }
     
    /** 
    * create  
    * 通过 call_user_func 获得本class的实例
    * 
    * @access public 
    * @since 1.0 
    * @return object 本类的实例
    */  
    public static function create(){  
        static $instance;
        if (!$instance){  
            $instance = new mod_poster_information_management();  
        }
        return $instance;  
    }   
 }
 

?>